package com.ruoyi.web.controller.customer;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.RestaurantBalanceDetail;
import com.ruoyi.common.core.domain.entity.RestaurantCustomer;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.service.RestaurantBalanceService;
import com.ruoyi.system.service.RestaurantCustomerService;
import com.ruoyi.system.service.resp.BalanceTodayStatDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Calendar;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: ruohao
 * @Date: 2023/9/11
 **/
@Api("用户消费接口")
@RestController
@RequestMapping("/shop/customer/balance")
public class BalanceDetailController extends BaseController {
    @Resource
    private RestaurantBalanceService restaurantBalanceService;
    @Resource
    private RestaurantCustomerService restaurantCustomerService;

    @ApiOperation("获取消费明细")
    @GetMapping("/list")
    public TableDataInfo list(RestaurantBalanceDetail restaurantBalanceDetail)
    {
        startPage();
        if (!"pc".equals(restaurantBalanceDetail.getAppSource())) {
            restaurantBalanceDetail.setCustomerId(SecurityUtils.getUserId());
        }
        List<RestaurantBalanceDetail> restaurantBalanceDetails = restaurantBalanceService.selectList(restaurantBalanceDetail);
        return getDataTable(restaurantBalanceDetails);
    }

    @ApiOperation("获取消费明细-积木报表")
    @GetMapping("/listPrint")
    public AjaxResult listPrint(RestaurantBalanceDetail restaurantBalanceDetail)
    {
        startPage();
        if (!"pc".equals(restaurantBalanceDetail.getAppSource())) {
            restaurantBalanceDetail.setCustomerId(SecurityUtils.getUserId());
        }
        List<RestaurantBalanceDetail> restaurantBalanceDetails = restaurantBalanceService.selectList(restaurantBalanceDetail);
        for (RestaurantBalanceDetail balanceDetail : restaurantBalanceDetails) {
            RestaurantCustomer customer = restaurantCustomerService.getCustomerByUserId(balanceDetail.getCustomerId().toString());
            balanceDetail.setMoney(customer.getBalance().setScale(2, RoundingMode.HALF_UP));
        }
        return AjaxResult.success(restaurantBalanceDetails);
    }

    @GetMapping("/getTodayStat")
    public AjaxResult getTodayStat()
    {
        RestaurantBalanceDetail query = new RestaurantBalanceDetail();
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 0); // 将时设置为0
        calendar.set(Calendar.MINUTE, 0);      // 将分设置为0
        calendar.set(Calendar.SECOND, 0);      // 将秒设置为0
        calendar.set(Calendar.MILLISECOND, 0);
        query.setCreateTime(calendar.getTime());
        List<RestaurantBalanceDetail> restaurantBalanceDetails = restaurantBalanceService.selectList(query);
        BalanceTodayStatDTO balanceTodayStatDTO = new BalanceTodayStatDTO();
        balanceTodayStatDTO.setTotalPeople(restaurantBalanceDetails.stream().map(RestaurantBalanceDetail::getCustomerId).collect(Collectors.toSet()).size());
        BigDecimal a = new BigDecimal(0);
        for (RestaurantBalanceDetail restaurantBalanceDetail : restaurantBalanceDetails) {
            a = a.add(restaurantBalanceDetail.getAmount() == null ? new BigDecimal(0) : restaurantBalanceDetail.getAmount());
        }
        balanceTodayStatDTO.setTotalAmount(a);
        balanceTodayStatDTO.setTotalOrder(restaurantBalanceDetails.size());
        balanceTodayStatDTO.setTotalCancel(0);
        return success(balanceTodayStatDTO);
    }

    @GetMapping("/listAll")
    public TableDataInfo listAll(RestaurantBalanceDetail restaurantBalanceDetail)
    {
        List<RestaurantBalanceDetail> restaurantBalanceDetails = restaurantBalanceService.selectList(restaurantBalanceDetail);
        return getDataTable(restaurantBalanceDetails);
    }

    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable Long id)
    {
        return success(restaurantBalanceService.selectById(id));
    }

    @ApiOperation("增加消费明细")
    @Log(title = "添加消费明细", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody RestaurantBalanceDetail restaurantBalanceDetail)
    {
        restaurantBalanceDetail.setCreateBy(SecurityUtils.getLoginUser().getUser().getNickName());
        Boolean result = restaurantBalanceService.create(restaurantBalanceDetail);
        if (result) {
            return success();
        }
        return error();
    }

    @Log(title = "修改消费明细", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody RestaurantBalanceDetail restaurantBalanceDetail) {
        restaurantBalanceDetail.setUpdateBy(SecurityUtils.getLoginUser().getUser().getNickName());
        Boolean result = restaurantBalanceService.update(restaurantBalanceDetail);
        if (result) {
            return success();
        }
        return error();
    }

    @Log(title = "用户信息导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RestaurantBalanceDetail customer) {
//        startPage();
        List<RestaurantBalanceDetail> list = restaurantBalanceService.selectList(customer);
        ExcelUtil<RestaurantBalanceDetail> util = new ExcelUtil<>(RestaurantBalanceDetail.class);
        util.exportExcel(response, list, "消费明细");
    }
}
